﻿using MyFirstWPF.View;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyFirstWPF.ViewModel
{
    public class StudyPlanDb : SQLiteConnection
    {
        public TableQuery<UserInfo> Users { get { return this.Table<UserInfo>(); } }

        public TableQuery<TaskInfo> Tasks { get { return this.Table<TaskInfo>(); } }

        public TableQuery<DiaryInfo> Diarys { get { return this.Table<DiaryInfo>(); } }

        public StudyPlanDb(string dbPath) : base(dbPath)
        {
            CreateTable<UserInfo>();
            CreateTable<TaskInfo>();
            CreateTable<DiaryInfo>();
        }

        public List<T> MyQuery<T>(Func<T,bool> func) where T : new()
        {
            using (var db = new StudyPlanDb(Page1.dbPath))
            {
                List<T> all = db.Table<T>().Where(x => true).ToList();
                LinkedList<T> linked = new LinkedList<T>();
                foreach(T t in all)
                {
                    if(func(t))
                    {
                        linked.AddLast(t);
                    }
                }
                return linked.ToList();
            }
        }
    }
}
